/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useState } from 'react';
import { Modal, message, Table, Button } from 'antd';
import { getDownloadTaskList } from '@/services/tools/qjOrder';

const columns = [
  {
    dataIndex: 'created_at',
    title: '任务开始时间',
    width: 400,
  },
  {
    dataIndex: 'status_desc',
    title: '任务状态',
    width: 340,
  },
  { dataIndex: 'file_path', title: '名称', width: 440 },
  {
    title: '操作类型',
    width: 340,
    render: function (value: string, d: any) {
      return d.status == 2 ? (
        <Button size='small' type='primary' onClick={() => window.open(d.file_url)}>
          导出
        </Button>
      ) : (
        <Button disabled>正在下载</Button>
      );
    },
  },
];

const Index: React.FC<{
  isOpened: boolean | undefined;
  onCancel: Function;
}> = (props) => {
  const { isOpened, onCancel } = props;
  const [data, setData] = useState();
  const [loading, setLoading] = useState(true);
  useEffect(() => {
    if (isOpened) {
      getDownloadTaskList().then(({ data: d, success }) => {
        setLoading(false);
        if (success) {
          setData(d);
        } else {
          message.warn('查询失败');
        }
      });
    }
  }, [isOpened]);
  const handleCancel = () => {
    onCancel();
  };

  return (
    <Modal
      cancelButtonProps={{
        style: { display: 'none' },
      }}
      visible={isOpened}
      onCancel={handleCancel}
      okButtonProps={{
        style: { display: 'none' },
      }}
      width={1000}
    >
      <Table loading={loading} dataSource={data} columns={columns} rowKey='id' />
    </Modal>
  );
};

export default Index;
